import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';

// import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { defHttp } from '/@/utils/http/axios';
import { downloadByUrl } from '/@/utils/file/download';
enum Api {
  PageList = '/gen/table/listByPage',
  Add = '/gen/table/add',
  Edit = 'gen/table/edit',
  Delete = 'gen/table/delete?id=',
  Createftl = 'gen/table/createftl',
  DbAllList = '/gen/data/all',
  TableAllList = '/gen/data/table',
}
// const { t } = useI18n();
const { createMessage } = useMessage();
const { success } = createMessage;
// 获取分页信息
export const getListByPage = (params?: any) => defHttp.get<any>({ url: Api.PageList, params });
// 获取数据源下来数据信息
export const getAllDbList = (params?: any) => defHttp.get<any>({ url: Api.DbAllList, params });
// 根据数据源获取表名列表
export const getAllTableList = (params?: any) =>
  defHttp.get<any>({ url: Api.TableAllList, params });
// 添加
export const addSpaUser = (params?: any) => {
  return defHttp.post<any>({ url: Api.Add, params }).then(() => {
    success('Success message');
  });
};
// 修改
export const editSpaUser = (params?: any) => {
  return defHttp.put<any>({ url: Api.Edit, params }).then(() => {
    success('Success message');
  });
};

// 删除字典
export const deleteSpaUser = (id, handleSuccess) => {
  return defHttp.delete({ url: Api.Delete + id }).then(() => {
    success('Success message');
    handleSuccess();
  });
};

// 生成代码
export const createftl = (params: any) => {
  return defHttp.post<any>({ url: Api.Createftl, params }).then(() => {
    success('Success message');
  });
};

// 列表
export const columns: BasicColumn[] = [
  {
    title: '数据源名称',
    dataIndex: 'dbName',
    width: 200,
  },
  {
    title: '表名',
    dataIndex: 'name',
    width: 200,
  },
  {
    title: '功能描述',
    dataIndex: 'comments',
    width: 200,
  },
  {
    title: 'className',
    dataIndex: 'className',
    width: 200,
  },
  {
    title: '模版',
    dataIndex: 'moduleName',
    width: 200,
  },
  {
    title: '模版',
    dataIndex: 'templateName',
    width: 200,
  },
  {
    title: '输出路径',
    dataIndex: 'outputDir',
    width: 200,
  },
  {
    title: '生成功能作者',
    dataIndex: 'functionAuthor',
    width: 200,
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 200,
  },
];

// 查询条件
export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '表名',
    component: 'Input',
    colProps: { span: 8 },
  },
];

// 编辑
export const formSchema: FormSchema[] = [
  {
    field: 'id',
    label: '唯一标识',
    required: false,
    component: 'Input',
    show: false,
  },
  {
    field: 'dbName',
    label: '',
    required: false,
    component: 'Input',
    show: false,
  },
  {
    field: 'dbId',
    label: '数据源名称',
    required: true,
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        showSearch: true,
        api: getAllDbList,
        labelField: 'dbName',
        valueField: 'id',
        //获取组件中的每一次修改的值
        onChange: (e, a) => {
          const { updateSchema } = formActionType;
          //重置变量
          formModel.name = undefined;
          formModel.dbName = a.label;
          getAllTableList({ dbId: e }).then((res) => {
            const optionCustomer = res.map((i) => {
              return {
                label: i,
                value: i,
              };
            });
            updateSchema({
              field: 'name',
              componentProps: () => {
                return {
                  //根据B组件类型赋值
                  options: optionCustomer,
                };
              },
            });
          });
        },
      };
    },
  },
  {
    field: 'name',
    label: '表名',
    required: true,
    component: 'Select',
  },
  {
    field: 'comments',
    label: '功能描述',
    required: true,
    component: 'Input',
  },
  {
    field: 'className',
    label: '实体类名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'moduleName',
    label: '生成模块名',
    required: false,
    component: 'Input',
  },
  {
    field: 'templateName',
    label: '模版',
    required: false,
    component: 'Input',
  },
  {
    field: 'outputDir',
    label: '输出路径',
    required: false,
    component: 'Input',
  },
  {
    field: 'functionAuthor',
    label: '生成功能作者',
    required: false,
    component: 'Input',
  },
];
